package edu.ucsd.cse110;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import edu.ucsd.cse110.model.Lift;

/**
 * TODO: Need documentation.
 */
public class LiftDetailActivity extends Activity {
	private Context mContext;
	private Bundle mExtras;
	private Lift mLift;
	private List<Lift> mListLift;
	private final int[] PICTURE_ID = { R.drawable.back_squat,
			R.drawable.benchpress, R.drawable.clean_and_jerk,
			R.drawable.deadlift, R.drawable.front_squat,
			R.drawable.overhead_squat, R.drawable.power_clean,
			R.drawable.power_snatch, R.drawable.push_jerk,
			R.drawable.push_press, R.drawable.shoulderpress,
			R.drawable.split_jerk, R.drawable.squat_clean,
			R.drawable.squat_snatch };
	private final int REQUEST_CODE_EDIT = 1;

	/**
	 * Displays the current rep.
	 */
	private void displayCurrentRep() {
		TextView txtDateOne = (TextView) findViewById(R.id.one_rep_date);
		TextView txtDateThree = (TextView) findViewById(R.id.three_rep_date);
		TextView txtDateFive = (TextView) findViewById(R.id.five_rep_date);
		TextView txtRepOne = (TextView) findViewById(R.id.one_rep);
		TextView txtRepThree = (TextView) findViewById(R.id.three_rep);
		TextView txtRepFive = (TextView) findViewById(R.id.five_rep);

		// One rep
		txtRepOne.setText(Integer.toString(mLift.getRepOne()));

		if (mLift.getDateOne().getTime() > 0) {
			txtDateOne.setText(Utility.dateToString(mLift.getDateOne(),
					"MM/dd/yyyy"));
		} else {
			txtDateOne.setText("N/A");
		}

		// Three rep
		txtRepThree.setText(Integer.toString(mLift.getRepThree()));

		if (mLift.getDateThree().getTime() > 0) {
			txtDateThree.setText(Utility.dateToString(mLift.getDateThree(),
					"MM/dd/yyyy"));
		} else {
			txtDateThree.setText("N/A");
		}

		// Five rep
		txtRepFive.setText(Integer.toString(mLift.getRepFive()));

		if (mLift.getDateFive().getTime() > 0) {
			txtDateFive.setText(Utility.dateToString(mLift.getDateFive(),
					"MM/dd/yyyy"));
		} else {
			txtDateFive.setText("N/A");
		}
	}

	/**
	 * Displays the personal record of this lift.
	 */
	private void displayPR() {
		TextView txtRepOnePR = (TextView) findViewById(R.id.one_rep_pr);
		TextView txtRepThreePR = (TextView) findViewById(R.id.three_rep_pr);
		TextView txtRepFivePR = (TextView) findViewById(R.id.five_rep_pr);

		txtRepOnePR.setText(Integer.toString(getRepOnePR()));
		txtRepThreePR.setText(Integer.toString(getRepThreePR()));
		txtRepFivePR.setText(Integer.toString(getRepFivePR()));
	}

	/**
	 * @return the greatest One Rep value ever recorded.
	 */
	private int getRepFivePR() {
		int repFivePR = 0;

		for (int i = 0; i < mListLift.size(); i++) {
			int repFive = mListLift.get(i).getRepFive();

			if (repFive > repFivePR) {
				repFivePR = repFive;
			}
		}

		return repFivePR;
	}

	/**
	 * @return the greatest One Rep value ever recorded.
	 */
	private int getRepOnePR() {
		int repOnePR = 0;

		for (int i = 0; i < mListLift.size(); i++) {
			int repOne = mListLift.get(i).getRepOne();

			if (repOne > repOnePR) {
				repOnePR = repOne;
			}
		}

		return repOnePR;
	}

	/**
	 * @return the greatest Three Rep value ever recorded.
	 */
	private int getRepThreePR() {
		int repThreePR = 0;

		for (int i = 0; i < mListLift.size(); i++) {
			int repThree = mListLift.get(i).getRepThree();

			if (repThree > repThreePR) {
				repThreePR = repThree;
			}
		}

		return repThreePR;
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE_EDIT) {
			if (resultCode == RESULT_OK) {
				Bundle extras = data.getExtras();

				long date = extras.getLong("Date");

				if (extras.getInt("RepOne") != 0) {
					mLift.setRepOne(extras.getInt("RepOne"));
					mLift.setDateOne(new Date(date));
				}
				if (extras.getInt("RepThree") != 0) {
					mLift.setRepThree(extras.getInt("RepThree"));
					mLift.setDateThree(new Date(date));
				}
				if (extras.getInt("RepFive") != 0) {
					mLift.setRepFive(extras.getInt("RepFive"));
					mLift.setDateFive(new Date(date));
				}

				Intent i = new Intent();
				i.putExtra("LiftId", mLift.getLiftId());
				i.putExtra("DateOne", mLift.getDateOne().getTime());
				i.putExtra("DateThree", mLift.getDateThree().getTime());
				i.putExtra("DateFive", mLift.getDateFive().getTime());
				i.putExtra("RepOne", mLift.getRepOne());
				i.putExtra("RepThree", mLift.getRepThree());
				i.putExtra("RepFive", mLift.getRepFive());
				setResult(RESULT_OK, i);

				mListLift.add(mLift);

				displayCurrentRep();
				displayPR();
			}
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.liftsdetail);

		mContext = this;
		mExtras = getIntent().getExtras();

		mLift = new Lift();
		mLift.setLiftId(mExtras.getInt("LiftId"));
		mLift.setName(mExtras.getString("Name"));
		mLift.setExecution(mExtras.getString("Execution"));
		mLift.setSetup(mExtras.getString("Setup"));
		mLift.setPicture(mExtras.getString("Picture"));
		mLift.setDateOne(new Date(mExtras.getLong("DateOne")));
		mLift.setDateThree(new Date(mExtras.getLong("DateThree")));
		mLift.setDateFive(new Date(mExtras.getLong("DateFive")));
		mLift.setRepOne(mExtras.getInt("RepOne"));
		mLift.setRepThree(mExtras.getInt("RepThree"));
		mLift.setRepFive(mExtras.getInt("RepFive"));

		LocalDatabaseManager database = new LocalDatabaseManager(this);
		mListLift = database.getLiftByLiftId(mLift.getLiftId());
		database.close();

		// Sets the name of the lift.
		TextView txtName = (TextView) findViewById(R.id.lifts_txt_title);
		txtName.setText(mLift.getName());

		// Sets the picture of the lift.
		ImageView imgPicture = (ImageView) findViewById(R.id.liftsdetail_img_picture);
		String strPicture = mLift.getPicture();

		String[] lifts = getResources().getStringArray(R.array.lift_pictures);

		for (int i = 0; i < lifts.length; i++) {
			if (strPicture.equals(lifts[i])) {
				imgPicture.setImageResource(PICTURE_ID[i]);
				break;
			}
		}

		// Sets the setup of the lift.
		TextView txtSetup = (TextView) findViewById(R.id.liftsdetail_txt_setup);
		txtSetup.setText(mLift.getSetup());

		// Sets the execution of the lift.
		TextView txtExecution = (TextView) findViewById(R.id.liftsdetail_txt_execution);
		txtExecution.setText(mLift.getExecution());

		displayCurrentRep();
		displayPR();
		Button btnEdit = (Button) findViewById(R.id.edit_button5);
		btnEdit.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(mContext, LiftEditButtonActivity.class);
				i.putExtra("RepOne", mLift.getRepOne());
				i.putExtra("RepThree", mLift.getRepThree());
				i.putExtra("RepFive", mLift.getRepFive());
				i.putExtra("LiftId", mLift.getLiftId());
				startActivityForResult(i, REQUEST_CODE_EDIT);
			}
		});
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		unbindDrawables(findViewById(R.id.RootView));
	}

	/**
	 * Frees up memory by destroying unnecessary pictures when not in use.
	 * 
	 * @param view
	 *            the context of which to modify.
	 */
	private void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
		}
	}
}
